﻿. u:\PSLogParser\LogParser_Lib.ps1

$inputFormat = New-Object -ComObject MSUtil.LogQuery.EASInputFormat
$outputFormat = New-Object -ComObject MSUtil.LogQuery.TemplateOutputFormat
$outputFormat.tpl = "EAS_User_Statistics.tpl"

$Location1 = "\\chw-cas-001\admin$\system32\LogFiles\W3SVC1\"
$Location2 = "\\chw-cas-002\admin$\system32\LogFiles\W3SVC1\"
$Location3 = "\\chw-owa-011\d$\OWALogfiles\W3SVC1\"
$OFS = ', '
$days = 0
$Files1 = @(Get-ChildItem $Location1 *.log | where {$_.LastWriteTime -gt (Get-Date).AddDays(-$days)} | foreach {$_.FullName})
$Files2 = @(Get-ChildItem $Location2 *.log | where {$_.LastWriteTime -gt (Get-Date).AddDays(-$days)} | foreach {$_.FullName})
$Files3 = @(Get-ChildItem $Location3 *.log | where {$_.LastWriteTime -gt (Get-Date).AddDays(-$days)} | foreach {$_.FullName})
$files1
$query = @"
SELECT  TOP 50
    iis-user-alias,
     iis-user-domain,
     eas-device-type,
     SUM (TotalClientEmailSync) AS TotalClientEmailSyncs,
     SUM (TotalServerEmailSync) AS TotalServerEmailSyncs,
     ADD (TotalClientEmailSyncs, TotalServerEmailSyncs) AS TotalEmailSyncs,
     SUM (TotalClientContactSync) AS TotalClientContactSyncs,
     SUM (TotalServerContactSync) AS TotalServerContactSyncs,
     ADD (TotalClientContactSyncs, TotalServerContactSyncs) AS TotalContactSyncs,
     SUM (TotalClientCalendarSync) AS TotalClientCalendarSyncs,
     SUM (TotalServerCalendarSync) AS TotalServerCalendarSyncs,
     ADD (TotalClientCalendarSyncs, TotalServerCalendarSyncs) AS TotalCalendarSyncs,
     ADD (TotalEmailSyncs, ADD (TotalContactSyncs, TotalCalendarSyncs)) AS TotalSyncs   
 
USING
 
     CASE eas-folder-data-type
          WHEN 'Em' THEN
                 ADD(eas-client-total-adds,
                 ADD (eas-client-total-changes,
                 ADD (eas-client-total-deletes, eas-client-total-fetches)))
          ELSE 0
     END AS TotalClientEmailSync,
 
     CASE eas-folder-data-type
          WHEN 'Em' THEN
                 ADD(eas-server-total-adds,
                 ADD (eas-server-total-changes, eas-server-total-deletes))
          ELSE 0
     END AS TotalServerEmailSync,
           
     CASE eas-folder-data-type
          WHEN 'Co' THEN
                 ADD(eas-client-total-adds,
                 ADD (eas-client-total-changes,
                 ADD (eas-client-total-deletes, eas-client-total-fetches)))
          ELSE 0
     END AS TotalClientContactSync,
 
     CASE eas-folder-data-type
          WHEN 'Co' THEN
                 ADD(eas-server-total-adds,
                 ADD (eas-server-total-changes, eas-server-total-deletes))
          ELSE 0
     END AS TotalServerContactSync,
 
     CASE eas-folder-data-type
          WHEN 'Ca' THEN
                 ADD(eas-client-total-adds,
                 ADD (eas-client-total-changes,
                 ADD (eas-client-total-deletes, eas-client-total-fetches)))
          ELSE 0
     END AS TotalClientCalendarSync,
 
     CASE eas-folder-data-type
          WHEN 'Ca' THEN
                 ADD(eas-server-total-adds,
                 ADD (eas-server-total-changes, eas-server-total-deletes))
          ELSE 0
     END AS TotalServerCalendarSync
 
INTO U:\ActiveSyncQuery\EAS_User_Statistics.html
 
FROM $files1,$files2,$files3
 
GROUP BY
     iis-user-alias,
     iis-user-domain,
     eas-device-type
 
 
ORDER BY
     TotalSyncs DESC
"@
